import util from "../../utils/util"
import api from "../../utils/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phonenumber:"",
    username:"",
    passworld:"",
    newpassworld:"",
    repeatpass:"",
    identifycode:"",
    isCanSubmit:false,
    identifyText:"获取验证码",
    imageidenfify:"",
    confirmText:"",
    role:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 提交注册信息
   */
  submit:function(){
    var that = this;
    //校验
    var flag = util.validate({
      username: this.data.username,
      phonenumber: this.data.phonenumber,
      passworld: this.data.passworld,
      identifycode: this.data.identifycode,
      repeatpass:this.data.repeatpass
    });
    if(flag){
      this.setData({
        confirmText: flag
      });
      return;
    }

    wx.request({
      url: api.retrievePwd,
      data:{
        userName:that.data.username,
        phone:that.data.phonenumber,
        newPwd:that.data.newpassworld,
        confirmPwd:that.data.repeatpass,
        sendSMS: that.data.identifycode
      },
      method:"POST",
      success:function(response){
        console.log("重置密码成功！");
        if(response.status){
          //成功
          //弹出提示
          wx.showModal({
            title: '提示',
            content: '修改成功！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: "../login/login"
                });
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else{
          //失败
          // debugger;
          that.setData({
            confirmText:response.data.infor
          });

        }
          
      },
      error:function(){

      }
    })


  },
  /**
   * 输入时自动同步
   */
  inputUsername: function (e) {
    this.setData({
      username: e.detail.value.trim()
    });
    this.canSubmit();
  },
  inputPhone:function(e) {
    this.setData({
      phonenumber:e.detail.value.trim()
    });
    this.canSubmit();
  },
  inputPassWorld:function (e) {
    this.setData({
      passworld:e.detail.value.trim()
    });
    this.canSubmit();
  },
    inputRepeatpass: function (e) {
    this.setData({
      repeatpass: e.detail.value.trim()
    });
    // debugger;
    this.canSubmit();
  },
  inputNewPass:function(e) {
  	this.setData({
      newpassworld: e.detail.value.trim()
    });
    this.canSubmit();
  },
  inputIdentifyCode:function(e){
    this.setData({
      identifycode:e.detail.value.trim()
    });
    this.canSubmit();
  },
  /**
   * 判断提交按钮是不是显示
   */
  canSubmit:function(){
    if(this.data.phonenumber && this.data.passworld&&this.data.identifycode&&this.data.repeatpass&&this.data.newpassworld&&this.data.username){
        //可以提交
        this.setData({
          isCanSubmit:true
        });
    }else{
      this.setData({
        isCanSubmit:false
      });
    }
  },
  /**
   * 点击获取验证码
   */
  getIdentifyCode:function(){
    console.log("成功发送验证码");
    //发送验证码
    util.sendIdentifyCode(this.data.phonenumber);
    //倒计时
    var seconds = 10;
    var that = this;
    var timer;
    if (this.data.identifyText == "获取验证码"){
       timer = setInterval(function () {
        seconds--;
        that.setData({
          identifyText: seconds
        });
        if (that.data.identifyText == "0"){
          clearInterval(timer);
          that.setData({
            identifyText:"获取验证码"
          });
        }
      }, 1000);
    }
    
  }
  

})